package com.health.mapp.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.health.db.model.UserAccountLedger;
import com.health.db.model.UserCardList;
import com.health.db.model.UserCardListExt;
import com.health.db.model.UserCardListTemp;
import com.health.db.service.user.IUserAccountLedgerService;
import com.health.db.service.user.IUserCardListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryAccountReq;
import com.health.mapp.bean.QueryAccountResp;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * @author HiRoad
 *
 */
public class QueryAccountImpl  implements ActionInterface {

	private static Logger logger = Logger.getLogger(QueryAccountImpl.class);
	
	private IUserLISTService userLISTService;
	private IUserCardListService userCardListService;
	private IUserAccountLedgerService userAccountLedgerService  ;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		QueryAccountReq req=(QueryAccountReq) reqBean;
		QueryAccountResp resp = new QueryAccountResp();
		
		
		
		if(StringUtils.isBlank(req.getUserId())&&StringUtils.isBlank(req.getDocId())){
			resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return resp;
		}
		
		BigDecimal cardLeftTime02=new BigDecimal("0");
		BigDecimal balance = new BigDecimal("0");
	  
		if (StringUtils.isNotBlank(req.getUserId())) {

			//查询用户卡
			UserCardListExt example =new UserCardListExt();
			example.setOwnerid(Long.valueOf(req.getUserId()));
			List<UserCardList>  list =userCardListService.selectUserCardLists(example);
		
			List<UserCardListTemp>  returnList = new ArrayList<UserCardListTemp>();
			
			for (int i = 0; i < list.size(); i++) {
				UserCardList userCardList = (UserCardList) list.get(i);
				UserCardListTemp returnTemp = new UserCardListTemp();
				if (null!=userCardList.getLefttime01()) returnTemp.setLeftTime01(userCardList.getLefttime01().intValue());
				if (null!=userCardList.getLefttime02()){
					cardLeftTime02=cardLeftTime02.add(userCardList.getLefttime02());
				}
				if (null!=userCardList.getLefttime03()) returnTemp.setLeftTime03(userCardList.getLefttime03().intValue());
				if (null!=userCardList.getLefttime04()) returnTemp.setLeftTime04(userCardList.getLefttime04().intValue());
				if (null!=userCardList.getLefttime05()) returnTemp.setLeftTime05(userCardList.getLefttime05().intValue());
				returnTemp.setCardId(userCardList.getCardIdno());
				returnTemp.setActiveTime(DateUtils.formatDatetime(userCardList.getActivateDate()));
				returnTemp.setFailureTime(DateUtils.formatDatetime(userCardList.getFailureDate()));
				returnTemp.setCardName(userCardList.getCardName());
				returnList.add(returnTemp);
				
			}
			balance = cardLeftTime02;
			resp.setCardList(returnList);
			/*UserAccountLedger  ledger= userAccountLedgerService.selectUserAccountLedgerByUserId(Long.valueOf(req.getUserId()));
			
			if (null!=ledger&&null!=ledger.getBalanceTotal()&&("2".equals(ledger.getAccountid()))) {
			//	resp.setBalance(ledger.getBalanceTotal().setScale(2));
				balance = cardLeftTime02.add(ledger.getBalanceTotal());
			}*/
			
			resp.setBalance(String.valueOf(balance.setScale(2,  BigDecimal.ROUND_HALF_UP)));
		}else{
			
			UserAccountLedger  ledger= userAccountLedgerService.selectUserAccountLedgerByUserId(Long.valueOf(req.getDocId()));
			
			if (null!=ledger&&null!=ledger.getBalanceTotal()) {
				//resp.setBalance(ledger.getBalanceTotal().setScale(2));
				resp.setBalance(String.valueOf(ledger.getBalanceTotal().setScale(2,  BigDecimal.ROUND_HALF_UP)));
			}
		}
		return resp;
	}
	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}
	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}
	/**
	 * @return the userCardListService
	 */
	public IUserCardListService getUserCardListService() {
		return userCardListService;
	}
	/**
	 * @param userCardListService the userCardListService to set
	 */
	public void setUserCardListService(IUserCardListService userCardListService) {
		this.userCardListService = userCardListService;
	}
	/**
	 * @return the userAccountLedgerService
	 */
	public IUserAccountLedgerService getUserAccountLedgerService() {
		return userAccountLedgerService;
	}
	/**
	 * @param userAccountLedgerService the userAccountLedgerService to set
	 */
	public void setUserAccountLedgerService(
			IUserAccountLedgerService userAccountLedgerService) {
		this.userAccountLedgerService = userAccountLedgerService;
	}
	
	
	
	
	
}
